package com.xiaoluo.lottery.service;

import com.xl.tool.redis.JedisResource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/4/14.
 */
@Component
public class OnlineNumManager {
    private ScheduledExecutorService threadPool= Executors.newSingleThreadScheduledExecutor();
    private int virtualNum;
    private static final Logger log= LoggerFactory.getLogger(OnlineNumManager.class);
    private static final List<OnlineNumConfig> onlineConfigList=new ArrayList<>();
    private static final int UPDATE_VIRTUAL_NUM_PERIOD=30;
    static {
        onlineConfigList.add(new OnlineNumConfig("09:00","12:00",300,500));
        onlineConfigList.add(new OnlineNumConfig("15:00","18:00",300,500));
        onlineConfigList.add(new OnlineNumConfig("18:30","24:00",400,600));
        onlineConfigList.add(new OnlineNumConfig("24:00","09:00",0,50));
    }
    public OnlineNumManager(){
        Runnable task=new Runnable() {
            @Override
            public void run() {
                Calendar calendar=Calendar.getInstance();
                int hour=calendar.get(Calendar.HOUR_OF_DAY);
                int min=calendar.get(Calendar.MINUTE);
                int currentValue=hour*60+min;
                int randNum=0;
                Random random=new Random();
                for(OnlineNumConfig config:onlineConfigList){
                    if(currentValue>=config.getStartValue()&&currentValue<config.getEndValue()){
                        randNum=config.minNum+random.nextInt(config.maxNum-config.minNum);
                        break;
                    }
                }
                if(randNum==0){
                    randNum=100+random.nextInt(100);
                }
                int virtualNum=OnlineNumManager.this.virtualNum;
                if(virtualNum==0){
                    OnlineNumManager.this.virtualNum=randNum;
                    return;
                }
                if(Math.abs(randNum-virtualNum)>50){
                    int rand=new Random().nextInt(50);
                    virtualNum=virtualNum+((randNum>virtualNum)?rand:-rand);
                }else{
                    virtualNum=randNum;
                }
                OnlineNumManager.this.virtualNum=virtualNum;
                log.info("Update virtual num {}",virtualNum);
            }
        };
        threadPool.scheduleWithFixedDelay(task,0,UPDATE_VIRTUAL_NUM_PERIOD, TimeUnit.SECONDS);
    }

    public int getVirtualNum() {
        return virtualNum;
    }

    public int getRealNum() {
        Jedis jedis= JedisResource.getInstance().get();
        Set<String> onlineOpenIdSet=jedis.keys(RoundServiceImpl.PLAYER_ONLINE_AGG+"*");
        JedisResource.getInstance().release();
        return onlineOpenIdSet.size();
    }
    public int getPubNum(){
        return getRealNum()+getVirtualNum();
    }

    public static class OnlineNumConfig {
        private int startHour;
        private int startMin;
        private int endHour;
        private int endMin;
        private int minNum;
        private int maxNum;
        public OnlineNumConfig(String start,String end,int minNum,int maxNum){
            String[] startArray=start.split(":");
            String[] endArray=end.split(":");
            this.startHour=Integer.parseInt(startArray[0]);
            this.startMin=Integer.parseInt(startArray[1]);
            this.endHour=Integer.parseInt(endArray[0]);
            this.endMin=Integer.parseInt(endArray[1]);
            this.minNum=minNum;
            this.maxNum=maxNum;
        }
        public int getStartValue(){
            return calValue(startHour,startMin);
        }
        public int getEndValue(){
            return calValue(endHour,endMin);
        }
        public static int calValue(int hour,int min){
            int value=hour*60+min;
            return value;
        }
    }
}
